use std::sync::mpsc;

pub struct Receiver<T> {
    recv:mpsc::Receiver<T>,
}

impl<T> Receiver<T> {
    pub fn new(recv:mpsc::Receiver<T>) -> Self {
        Self {
            recv:recv,
        }
    }
}

impl<T> IntoIterator for Receiver<T> {
    type Item = T;
    type IntoIter = mpsc::IntoIter<T>;

    fn into_iter(self) -> Self::IntoIter {
        self.recv.into_iter()
    }
}

pub struct Sender<T> {
    send:mpsc::Sender<T>,
}

impl<T> Sender<T> {
    pub fn new(send:mpsc::Sender<T>) -> Self {
        Self {
            send:send,
        }
    }

    pub fn send(&self,t:T) {
        self.send.send(t).expect("failed send");
    }
}

pub fn channel<T>() -> (Sender<T>,Receiver<T>) {
    let (tx,rx) = mpsc::channel();
    (Sender::new(tx),Receiver::new(rx))
}